package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.RemotePindao;
import com.flymobi.util.QL;

public class RemotePindaoService extends CommonService {

	public List<RemotePindao> list() {
		return dao.find(RemotePindao.class, QL.HQL.RemotePindao.LIST, new Object[] {});
	}
	
	public List<RemotePindao> listShow() {
		return dao.find(RemotePindao.class, QL.HQL.RemotePindao.LIST_SHOW, new Object[] {});
	}
	
	public List<RemotePindao> listRelation(Long pindaoId) {
		return dao.find(RemotePindao.class, QL.HQL.RemotePindao.LIST_BY_PINDAOID, new Object[] {pindaoId});
	}
	
	public List<RemotePindao> list(Long fatherid) {
		return dao.find(RemotePindao.class, QL.HQL.RemotePindao.LIST_SUB, new Object[] {fatherid});
	}
	
	public List<RemotePindao> listShow(Long fatherid) {
		return dao.find(RemotePindao.class, QL.HQL.RemotePindao.LIST_SUB_SHOW, new Object[] {fatherid});
	}


	public void save(RemotePindao RemotePindao) {
		dao.save(RemotePindao);
	}

	public RemotePindao load(String id) {
		return dao.load(RemotePindao.class, new Long(id));
	}
	
	public RemotePindao get(String id) {
		return dao.get(RemotePindao.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
	
	public void updateUp(String id) {
		RemotePindao pe = this.get(id);
		RemotePindao peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}
	
	public void updateDown(String id) {
		RemotePindao pe = this.get(id);
		RemotePindao peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(RemotePindao pe, RemotePindao peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private RemotePindao findUp(RemotePindao pe) {
		return dao.findSingle(RemotePindao.class, QL.HQL.RemotePindao.LIST_UP, new Object[] {
			pe.getFatherId(), pe.getIdx() });
	}
	
	private RemotePindao findDown(RemotePindao pe) {
		return dao.findSingle(RemotePindao.class, QL.HQL.RemotePindao.LIST_DOWN, new Object[] {
			pe.getFatherId(), pe.getIdx() });
	}
	
	public Long getSeq(Long fatherid){
		return dao.getSeq(QL.HQL.RemotePindao.GET_MAX_IDX, new Object[]{fatherid});
	}
}
